package cn.dgut.changeManager;

import cn.dgut.Utils.JDBCUtils;
import sun.net.www.content.image.png;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.*;

@WebServlet("/AddInfo")
public class AddInfoServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        Map<String, String[]> parameterMap = request.getParameterMap();
        List<String> list = new ArrayList<>();

        for (Iterator iter = parameterMap.entrySet().iterator(); iter.hasNext(); ) {
            Map.Entry element = (Map.Entry) iter.next();
            String[] value = (String[]) element.getValue();
            for (int i = 0; i < value.length; i++) {
                list.add(value[i]);
            }
        }
        int page = Integer.parseInt(list.get(0));
        String sql = null;
        if(page==0){
            sql = "INSERT INTO `User` VALUES (null,'"+list.get(1)+"','"+list.get(2)+"', '"+list.get(3)+"', '"+list.get(4)+"', 'default.png ');";
        }else if(page==1){
            sql = "INSERT INTO `Student` VALUES (null,'"+list.get(1)+"', '"+list.get(2)+"','"+list.get(3)+"', '"+list.get(4)+"', 'default.png','"+list.get(5)+"','"+list.get(6)+"');";
        }else if(page==2){
            sql = "INSERT INTO `Teacher` VALUES (null,'"+list.get(1)+"', '"+list.get(2)+"','"+list.get(3)+"', '"+list.get(4)+"', 'default.png','"+list.get(5)+"','"+list.get(6)+"','"+list.get(7)+"','2');";
        }else if(page==3){
            sql="";
        }else if(page==4){
            sql = "INSERT INTO `Select_Course` VALUES (null,"+list.get(1)+","+list.get(2)+", '"+list.get(3)+"');";
        }else if(page==5){
            Date date = new Date();
            SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String time = dateFormat.format(date);
            sql = "INSERT INTO `StudentWorks` VALUES (null,'"+list.get(1)+"', 0 , '"+time+"' ,'"+list.get(2)+"');";
        }else if(page==6){
            sql = "INSERT INTO `Recruit` VALUES (null,'"+list.get(1)+" ', '"+list.get(2)+"', '"+list.get(3)+"', '"+list.get(4)+"');";
        }else if(page==7){
            sql = "INSERT INTO `Teacher` VALUES (null,'"+list.get(2)+"', '"+list.get(2)+"','"+list.get(3)+"', '"+list.get(4)+"', 'default.png','"+list.get(5)+"','"+list.get(6)+"','"+list.get(7)+"','0');";
        }else {
            return;
        }
        JDBCUtils jdbcUtils = new JDBCUtils();
        int status = jdbcUtils.querySQL(sql);
        HttpSession session = request.getSession(true);
        session.setAttribute("isRight", status);
        response.sendRedirect("/backstage/main_page.jsp?page="+page);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}